package util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.commons.dbcp.BasicDataSource;

public class DBUtil {


	private final static String className = "oracle.jdbc.OracleDriver";
	private final static String url = "jdbc:oracle:thin:@ZHA-ITA092-W7:1522:orcl";
	private final static String username = "lionel";
	private final static String password = "123456";
	private static BasicDataSource bds;

	static {
		bds = new BasicDataSource();
		bds.setDriverClassName(className);
		bds.setUrl(url);
		bds.setUsername(username);
		bds.setPassword(password);
		bds.setMaxActive(10);
	}

	/**
	 * get connection
	 * 
	 * @return
	 */
	public static Connection getConn() {
		Connection conn = null;
		try {
			Class.forName(className);
			conn = DriverManager.getConnection(url, username, password);
		} catch (SQLException e) {
			System.out
					.println("... url or username or password is not found ...");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			System.out.println("... class is not found ...");
		}
		return conn;
	}

	/**
	 * get connection from dataSource
	 * 
	 * @return
	 */
	public static Connection getConnectionWithDs() {
		try {
			return bds.getConnection();
		} catch (SQLException e) {
			e.printStackTrace();
			System.out.println("sorry the dataSource error");
			return null;
		}
	}

	/**
	 * close the resource
	 */
	public static void close(Connection conn, PreparedStatement pst,
			ResultSet rs) {
		try {
			if (rs != null)
				rs.close();
			if (pst != null)
				pst.close();
			if (conn != null)
				conn.close();
		} catch (SQLException e) {
			System.out.println("close exception");
		}
	}



}
